#
import unittest
import random
import torch
from apps.dhlp.dhlp_ds import DhlpDs
from apps.dhlp.data_vis import DataVis

class TDhlpDs(unittest.TestCase):
    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_get_max_recs_dev
    def test_get_max_recs_dev(self):
        # 1. 找到记录数最多的设备
        # 1.1. 读入：{rec_id, {数据记录描述信息：开始时间、地点......}}
        csv_fn = 'work/datas/v0/time_periods.csv'
        rec_db = DataVis.get_rec_db(csv_fn=csv_fn)
        # 1.2. 读入: {'devid': {'tls': [雷电原始数据ID列表], 'norms': [正常数据ID列表]}}
        dev_recs = DataVis.get_devs_recs(csv_fn=csv_fn)
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        print(f'记录数最多的设备：{did}; 雷电数量为：{len(dev_recs[did]["tls"])}; 正常记录：{dev_recs[did]["norms"]};')

    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_get_recs
    def test_get_recs(self):
        rec_db, dev_recs, raw_datas = DhlpDs.load_raw_datas()
        # 1. 找到记录数最多的设备
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        recs, labels = DhlpDs.get_recs(did=did, dev_recs=dev_recs, raw_datas=raw_datas)
        print(f'共有{len(recs)}条记录，类型为：{type(recs[0])};')
        print(f'labels: {labels};')

    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_generate_norm_recs
    def test_generate_norm_recs(self):
        rec_db, dev_recs, raw_datas = DhlpDs.load_raw_datas()
        # 1. 找到记录数最多的设备
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        recs, labels = DhlpDs.get_recs(did=did, dev_recs=dev_recs, raw_datas=raw_datas)
        norm_recs, norm_labels = DhlpDs.generate_norm_recs(recs)
        print(f'正常记录数: norm_recs: {len(norm_recs)}; ????????????????????')
        print(f'正常记录标签: {norm_labels};')

    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_split_train_test
    def test_split_train_test(self):
        rec_db, dev_recs, raw_datas = DhlpDs.load_raw_datas()
        # 1. 找到记录数最多的设备
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        tls_recs, tls_labels = DhlpDs.get_recs(did=did, dev_recs=dev_recs, raw_datas=raw_datas)
        norm_recs, norm_labels = DhlpDs.generate_norm_recs(tls_recs)
        recs = tls_recs + norm_recs
        labels = tls_labels + norm_labels
        train_datas, train_labels, test_datas, test_labels = DhlpDs.split_train_test(recs=recs, labels=labels)
        print(f'训练集: {len(train_datas)}; 测试集: {len(test_datas)};')

    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_generate_frames
    def test_generate_frames(self):
        rec_db, dev_recs, raw_datas = DhlpDs.load_raw_datas()
        # 1. 找到记录数最多的设备
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        tls_recs, tls_labels = DhlpDs.get_recs(did=did, dev_recs=dev_recs, raw_datas=raw_datas)
        norm_recs, norm_labels = DhlpDs.generate_norm_recs(tls_recs)
        recs = tls_recs + norm_recs
        labels = tls_labels + norm_labels
        train_datas, train_labels, test_datas, test_labels = DhlpDs.split_train_test(recs=recs, labels=labels)
        frames, frame_lables = DhlpDs.generate_frames(train_datas=train_datas, train_labels=train_labels)

    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_get_mu_std
    def test_get_mu_std(self):
        rec_db, dev_recs, raw_datas = DhlpDs.load_raw_datas()
        # 1. 找到记录数最多的设备
        did = DhlpDs.get_max_recs_dev(dev_recs=dev_recs)
        tls_recs, tls_labels = DhlpDs.get_recs(did=did, dev_recs=dev_recs, raw_datas=raw_datas)
        norm_recs, norm_labels = DhlpDs.generate_norm_recs(tls_recs)
        recs = tls_recs + norm_recs
        labels = tls_labels + norm_labels
        train_datas, train_labels, test_datas, test_labels = DhlpDs.split_train_test(recs=recs, labels=labels)
        frames, frame_labels = DhlpDs.generate_frames(train_datas=train_datas, train_labels=train_labels)
        tlf_mu, tlf_std = DhlpDs.get_mu_std(frames=frames, frame_labels=frame_labels)
        print(f'### mean: {tlf_mu}; std: {tlf_std}; ???????????????????')



    # python -m unittest -v uts.apps.dhlp.t_dhlp_ds.TDhlpDs.test_t001
    def test_t001(self):
        v1 = torch.zeros(10,)
        v2 = torch.zeros(10,)
        v = []
        v.append(v1)
        print(f'step 1 {v};')
        v.append(v2)
        print(f'step 2 {v};')
        # v3 = torch.tensor(v)
        v3 = v1
        v3 = torch.hstack((v3, v2))
        print(f'{v1.shape} + {v2.shape} => {v3.shape};')
